import LayoutBox from './components/Layout';
import {useRouter} from 'next/router';
import {useEffect} from 'react';
import App from 'next/app';
import http from "../services/http";
import {message} from "antd";
import '../globals.css'

function myApp({Component, pageProps}) {

    const router = useRouter();

    useEffect(() => {
        const token = localStorage.getItem('token');
        if (!token) {
            // 检查是否已在登录页，避免无限重定向
            if (router.pathname !== '/login') {
                router.push('/login');
            }
        }
    }, [router]);

    // 根据当前路由决定是否使用 LayoutBox 布局
    const isLogin = router.pathname === '/login';

    return (
        <div>
            {isLogin ? (
                <Component {...pageProps} />
            ) : (
                <LayoutBox>
                    <Component {...pageProps} />
                </LayoutBox>
            )}
        </div>
    );
}

myApp.getInitialProps = async (appContext) => {
    // 这里可以进行服务器端的 Token 验证
    const appProps = await App.getInitialProps(appContext);
    return {...appProps}
}

export default myApp;
